#include "Bike.h"

Bike::Bike(float s, TextureDrawer* td){
	scale = s;
	wireFrame = 0;
	texture = td;
	showTexture = true;
}

// Alpha, translation X, translation Y, translation Z, rotation X, rotation Y, rotation Z
void Bike::draw(float a, float tX, float tY, float tZ, float rX, float rY, float rZ){
	glPushMatrix();
	glTranslatef(tX, tY, tZ);
	glScalef(scale, scale,scale);
	glRotated(rX, 1, 0, 0);
	glRotated(rY, 0, 1, 0);
	glRotated(rZ, 0, 0, 1);
	drawMotorcycle(a);
	glPopMatrix();
}

void Bike::changeWireFrame(){
	wireFrame = 1-wireFrame;
}

void Bike::changeShowTexture(){
	showTexture = !showTexture;
}

void Bike::changeScale(float s){
	scale = s;
}

// Function to draw the motorcycle
void Bike::drawMotorcycle(float alpha){
	drawMotorcycleFrame(alpha);
	drawMotorcycleEngine(alpha);
	drawMotorcycleExhaust(alpha);
	drawMotorcycleGasTank(alpha);
	drawMotorcycleHandleBars(alpha);
	drawMotorcycleLights(alpha);
	drawMotorcycleWheels(alpha);
}

// Function to draw the motorcycle's frame
void Bike::drawMotorcycleFrame(float alpha){
	// Motorcycle Part 1: Frame
    glPushMatrix();                  // Base body
	glTranslatef(.15, -0.1, 0);
    glScalef(.95,.1,.2);
	glColor4f(0.2,0.2,0.2, alpha);
	if(wireFrame==1)
		glutWireCube(.45);
	else{
		glutSolidCube (.45);
	}
	glColor4f(1,1,1, alpha);
	
	glTranslatef(0, 1.7, 0);	// Seat
	glScalef(.7,2,1.2);
	glColor4f(0,0,0, alpha);
	if(wireFrame==1)
		glutWireSphere(.3, 10, 30);
	else{
		if(showTexture){
			glColor4f(1,1,1, alpha);
			texture->drawTexturedSphere(3, .3, 10, 30);
		}
		else{
			glutSolidSphere(.3, 10, 30);
		}	
	}
	glColor4f(1,1,1, alpha);

	glTranslatef(-.42, .1, 0);  // Seat back frame
    glScalef(.9, .8, 1);	
	glColor4f(0,0,1,alpha);
	if(wireFrame==1)		
		glutWireCube(.45);
	else{
		if(showTexture){
			glColor4f(1,1,1, alpha);
			texture->drawTexturedCube(0, .45);
		}
		else{
			glutSolidCube (.45);
		}
	}
	glColor4f(1,1,1,alpha);
    glPopMatrix();

	// Back Frame
	glPushMatrix(); 
	glTranslatef(-.1, 0, 0);
	glRotated(-50, 0, 0, 1);
    glScalef(.56, .2,.15);
	glColor4f(0,0,0,alpha);
	if(wireFrame==1)
		glutWireCube(.45);
	else{
		glutSolidCube (.45);
	}
	glColor4f(1,1,1,alpha);
	glPopMatrix();

	//Front wheel frame
	glPushMatrix(); 
	glTranslatef(.5, .15, 0);
	glRotated(-70, 0, 0, 1);
	glScalef(.7,.25,.3);
	glColor4f(0,0,1,alpha);
	if(wireFrame==1)		// Front wheel main bar
	   glutWireCube(.3);
    else{
		if(showTexture){
			glColor4f(1,1,1, alpha);
			texture->drawTexturedCube(0, .3);
		}
		else{
			if(showTexture){
			glColor4f(1,1,1, alpha);
			texture->drawTexturedCube(0, .3);
		}
		else{
			glutSolidCube (.3);
		}
		}
	}

	glTranslatef(0.25, 0, 0.2);
	glRotated(-5, 0, 0, 1);
	glScalef(1.5,.5,.2);
	glColor4f(0.5,0.5,0.5, alpha);
	if(wireFrame==1)		// Front wheel right side connection
	   glutWireCube(.3);
    else{
		if(showTexture){
			glColor4f(1,1,1, alpha);
			texture->drawTexturedCube(7, .3);
		}
		else{
			glutSolidCube (.3);
		}
	}

	glTranslatef(0, 0, -2);
	if(wireFrame==1)		// Front wheel left side connection
	   glutWireCube(.3);
    else{
		if(showTexture){
			glColor4f(1,1,1, alpha);
			texture->drawTexturedCube(7, .3);
		}
		else{
			glutSolidCube (.3);
		}
	}
	glColor4f(1,1,1,alpha);
	glPopMatrix();

	glPushMatrix(); 
	glTranslatef(.5, .09, 0);
	glRotated(-70, 0, 0, 1);
	glScalef(.3,.25,.4);
	glColor4f(0.5,0.5,0.5,alpha);
	if(wireFrame==1)		// Front wheel main bar connection
	   glutWireCube(.3);
    else{
		if(showTexture){
			glColor4f(1,1,1, alpha);
			texture->drawTexturedCube(0, .3);
		}
		else{
			glutSolidCube (.3);
		}
	}
	glColor4f(1,1,1,alpha);
	glPopMatrix();

	// Front wheel frame connection to main frame
	glPushMatrix(); 
	glTranslatef(.4, .05, 0);
	glRotated(60, 0, 0, 1);
	glScalef(.95,.25,.25);
	glColor4f(0.7,0.7,0.7,alpha);
	if(wireFrame==1)		
	   glutWireCube(.3);
    else{
		if(showTexture){
			glColor4f(1,1,1, alpha);
			texture->drawTexturedCube(0, .3);
		}
		else{
			glutSolidCube (.3);
		}
	}
	glColor4f(1,1,1,alpha);
	glPopMatrix();

	// Back wheel connections to frame
	glPushMatrix(); 
	glRotated(18, 0, 0, 1);
	glTranslatef(-.15, -.05, 0.075);
	glScalef(1.35,.2,.15);
	glColor4f(0.5,0.5,0.5,alpha);
	if(wireFrame==1)				// right side
	   glutWireCube(.3);
    else{
		glutSolidCube (.3);
	}
	
	glTranslatef(0, 0, -1);
	if(wireFrame==1)				//left side
	   glutWireCube(.3);
    else{
		glutSolidCube (.3);
	}
	glColor4f(1,1,1,alpha);
	glPopMatrix();
}

// Function to draw the motorcycle's engine
void Bike::drawMotorcycleEngine(float alpha){
	// Motorcycle Part 2: Engine
    glPushMatrix();     
	glTranslatef(.23, 0, 0);	// Engine core
    glScalef(.45,.35,.25);	
	glColor4f(0.5,0.5,0.5,alpha);
	if(wireFrame==1)
		glutWireCube(.45);
	else{
		if(showTexture){
			glColor4f(1,1,1, alpha);
			glutSolidCube (.45);
		}
		else{
			glutSolidCube (.45);
		}
	}
	glColor4f(1,1,1,alpha);
    glPopMatrix();

	// Engine Details - Part 1
	glPushMatrix();
	glTranslatef(.27, 0.03, 0);
	glRotated(90, 0,0,1);
    glScalef(.025,.25,.3);
	glColor4f(0.2,0.2,0.2,alpha);
	if(wireFrame==1)
		glutWireCube(.45);
	else{
		glutSolidCube (.45);
	}

	glTranslatef(-1, 0, 0);
	if(wireFrame==1)
		glutWireCube(.45);
	else{
		glutSolidCube (.45);
	}

	glTranslatef(-1, 0, 0);
	if(wireFrame==1)
		glutWireCube(.45);
	else{
		glutSolidCube (.45);
	}

	glTranslatef(-1, 0.1, 0);
	if(wireFrame==1)
		glutWireCube(.45);
	else{
		glutSolidCube (.45);
	}
	glColor4f(1,1,1,alpha);
	glPopMatrix();

	// Engine Details - Part 2
	glPushMatrix(); 
	glTranslatef(0.21, 0.02, 0.05);
	glRotated(180, 1,0,0);
	glRotated(-20, 1,0,0);
	glScalef(.2,.2,.2);
	glColor4f(0.2,0.2,0.2,alpha);
	if(wireFrame==1)
	   glutWireCone(.1, .2, 30, 30);
    else{
		glutSolidCone(.1, .2, 30, 30);
	}

	if(wireFrame==1)
	   glutWireSphere(.1, 10, 30);
    else{
		glutSolidSphere(.1, 10, 30);
	}
	glColor4f(1,1,1,alpha);
	glPopMatrix();

	glPushMatrix(); 
	glTranslatef(0.21, -0.02, -0.05);
	glRotated(-20, 1,0,0);
	glScalef(.2,.2,.2);
	glColor4f(0.2,0.2,0.2,alpha);
	if(wireFrame==1)
	   glutWireCone(.1, .2, 30, 30);
    else{
		glutSolidCone (.1, .2, 30, 30);
	}

	if(wireFrame==1)
	   glutWireSphere(.1, 10, 30);
    else{
		glutSolidSphere(.1, 10, 30);
	}
	glColor4f(1,1,1,alpha);
	glPopMatrix();

	// Engine Details - Part 3
	glPushMatrix(); 
	glTranslatef(0.22, 0, 0.05);
	glRotated(180, 1,0,0);
	glRotated(-20, 1,0,0);
	glScalef(.2,.2,.2);
	glColor4f(0.2,0.2,0.2,alpha);
	if(wireFrame==1)
	   glutWireCone(.1, .2, 30, 30);
    else{
		glutSolidCone(.1, .2, 30, 30);
	}

	if(wireFrame==1)
	   glutWireSphere(.1, 10, 30);
    else{
		glutSolidSphere (.1, 10, 30);
	}
	glColor4f(1,1,1,alpha);
	glPopMatrix();

	// Engine Details - Part 4 
	glPushMatrix(); // Connect to exhaust pipe
	glColor4f(0.2,0.2,0.2,alpha);
	glTranslatef(0.15, -0.04, .08);
    glScalef(.05,.05,.1);
	if(wireFrame==1)
		glutWireCube(.45);
	else{
		glutSolidCube (.45);
	}
	glColor4f(1,1,1,alpha);
	glPopMatrix();
}

// Function to draw the motorcycle's exhaust
void Bike::drawMotorcycleExhaust(float alpha){
	// Motorcycle Part 3: Exhaust
	// Exhaust Core / Seat Base
	glPushMatrix(); 
	glTranslatef(.03, -0.015, 0);	// Core
    glScalef(.45,.3,.25);	
	glColor4f(0,0,0,alpha);
	if(wireFrame==1)
		glutWireCube(.45);
	else{
		glutSolidCube (.45);
	}
	glColor4f(1,1,1,alpha);
	glPopMatrix();

	// Exhaust Pipe
	glPushMatrix(); 
	glTranslatef(-0.05, -0.04, 0.08);	// Main Pipe
    glScalef(.9,.05,.03);	
	glColor4f(0.2,0.2,0.2,alpha);
	if(wireFrame==1)
		glutWireCube(.45);
	else{
		glutSolidCube (.45);
	}
	glColor4f(1,1,1,alpha);
	glPopMatrix();
}

// Function to draw the motorcycle's gas tank 
void Bike::drawMotorcycleGasTank(float alpha){
	// Motorcycle Part 5: Gas Tank
	glPushMatrix(); 
	glTranslatef(.3, .1, 0);
	glScalef(1.2,0.7,1.0);
	glColor4f(1,1,0,alpha);
	if(wireFrame==1)			// Gas Tank
	   glutWireSphere(.1, 10, 30);
    else{
		if(showTexture){
			glColor4f(1,1,1, alpha);
			texture->drawTexturedSphere(0, .1, 10, 30);
		}
		else{
			glutSolidSphere(.1, 10, 30);
		}
	}
	glColor4f(1,1,1,alpha);

	glTranslatef(.05, 0.08, 0);
	glScalef(.5,.5,.5);
	if(wireFrame==1)			// Gas Tank Cap
	   glutWireSphere(.05, 10, 30);
    else{
		if(showTexture){
			texture->drawTexturedSphere(5, .05, 10, 30);
		}
		else{
			glutSolidSphere(.05, 10, 30);
		}
	}
	glColor4f(1,1,1,alpha);
	glPopMatrix();
}

// Function to draw the motorcycle's handle bars
void Bike::drawMotorcycleHandleBars(float alpha){
	glPushMatrix();

	// Motorcycle Part 6: Handle bars
	glPushMatrix();				// Base
	glTranslatef(0.45, .22, 0);
	glScalef(.05,.05, .5);
	glColor4f(0.5,0.5,0.5,alpha);
	if(wireFrame==1)
	   glutWireCube(.3);
    else{
		glutSolidCube (.3);
	}
	glColor4f(1,1,1,alpha);
	glPopMatrix();

	glPushMatrix();				// Right bar
	glTranslatef(0.4, .22, 0.1);
	glRotated(-60, 0, 1, 0);
	glScalef(.05,.05, .4);
	glColor4f(0.5,0.5,0.5,alpha);
	if(wireFrame==1)
	   glutWireCube(.3);
    else{
		if(showTexture){
			glColor4f(1,1,1, alpha);
			texture->drawTexturedCube(7, .3);
		}
		else{
			glutSolidCube (.3);
		}
	}
	glColor4f(1,1,1,alpha);

	glTranslatef(0, 0, .15);	// Handle
	glScalef(.5,.5, .25);
	glColor4f(0.2,0.2,0.2,alpha);				
	if(wireFrame==1)					
	   glutWireSphere(.5, 30, 30);
    else{
		if(showTexture){
			texture->drawTexturedSphere(6, .5, 30, 30);
		}
		else{
			glColor4f(0,0,0,alpha);
			glutSolidSphere(.5, 30, 30);
		}
	}
	glColor4f(1,1,1,alpha);
	glPopMatrix();

	glPushMatrix();				// Right Rear view mirror
	glTranslatef(0.4, .25, 0.1); // Bar
	glRotated(-60, 1, 1, 0);
	glScalef(.05,.04, .25);
	glColor4f(0.5,0.5,0.5,alpha);
	if(wireFrame==1)
	   glutWireCube(.3);
    else{
		if(showTexture){
			glColor4f(1,1,1, alpha);
			texture->drawTexturedCube(7, .3);
		}
		else{
			glutSolidCube (.3);
		}
	}
	glColor4f(1,1,1,alpha);

	glTranslatef(0, 0, .2);	// Mirror
	glRotated(-90, 0, 0, 1);
	glScalef(1,.2, .2);
	glColor4f(1,1,1,alpha);				
	if(wireFrame==1)					
	   glutWireSphere(.5, 30, 30);
    else{
		if(showTexture){
			texture->drawTexturedSphere(7, .5, 30, 30);
		}
		else{
			glutSolidSphere(.5, 30, 30);
		}
	}
	glColor4f(1,1,1,alpha);
	glPopMatrix();

	glPushMatrix();				// Left bar
	glTranslatef(0.4, .22, -0.1);
	glRotated(60, 0, 1, 0);
	glScalef(.05,.05, .4);
	glColor4f(0.5,0.5,0.5,alpha);
	if(wireFrame==1)
	   glutWireCube(.3);
    else{
		if(showTexture){
			glColor4f(1,1,1, alpha);
			texture->drawTexturedCube(7, .3);
		}
		else{
			glutSolidCube (.3);
		}
	}
	glColor4f(1,1,1,alpha);

	glTranslatef(0, 0, -.15);	// Handle
	glScalef(.5, .5, .25);
	glColor4f(0.2,0.2,0.2,alpha);				
	if(wireFrame==1)					
	   glutWireSphere(.5, 30, 30);
    else{
		if(showTexture){
			texture->drawTexturedSphere(6, .5, 30, 30);
		}
		else{
			glColor4f(0,0,0,alpha);
			glutSolidSphere(.5, 30, 30);
		}
	}
	glColor4f(1,1,1,alpha);
	glPopMatrix();

	glPushMatrix();				// Left Rear view mirror
	glTranslatef(0.4, .25, -0.1);	// Bar
	glRotated(60, 1, 1, 0);
	glScalef(.05,.04, .25);
	glColor4f(0.5,0.5,0.5,alpha);
	if(wireFrame==1)
	   glutWireCube(.3);
    else{
		if(showTexture){
			glColor4f(1,1,1, alpha);
			texture->drawTexturedCube(7, .3);
		}
		else{
			glutSolidCube (.3);
		}
	}
	glColor4f(1,1,1,alpha);

	glTranslatef(0, 0, -.2);	// Mirror
	glRotated(-90, 0, 0, 1);
	glScalef(1,.2, .2);
	glColor4f(1,1,1,alpha);				
	if(wireFrame==1)					
	   glutWireSphere(.5, 30, 30);
    else{
		if(showTexture){
			texture->drawTexturedSphere(7, .5, 30, 30);
		}
		else{
			glutSolidSphere(.5, 30, 30);
		}
	}
	glColor4f(1,1,1,alpha);
	glPopMatrix();
	glPopMatrix();
}

// Function to draw the motorcycle's lights
void Bike::drawMotorcycleLights(float alpha){
	// Motorcycle Part 7: Lights
	glPushMatrix();				// Front Light
	glTranslatef(.55, .2, 0);
	glRotated(-90, 0, 1, 0);
	glScalef(.5, .5, .5);
	glColor4f(0.5,0.5,0.5,alpha);
	if(wireFrame==1)
	   glutWireCone(.1, .2, 30, 30);
    else{
		glutSolidCone(.1, .2, 30, 30);
	}
	glColor4f(1,1,1,alpha);

	glScalef(1, 1, .5);
	glColor4f(1,1,0,alpha);	
	if(wireFrame==1)
	   glutWireSphere(.11, 10, 10);
    else{
		if(showTexture){
			glColor4f(1,1,1, alpha);
			texture->drawTexturedSphere(5, .11, 10, 10);
		}
		else{
			glutSolidSphere(.11, 10, 10);
		}
	}
	glColor4f(1,1,1,alpha);
	glPopMatrix();

	glPushMatrix();					// Back Light
	glTranslatef(-.28, .08, 0);
	glRotated(-270, 0, 1, 0);
	glScalef(.5, .5, .5);
	glColor4f(0.5,0.5,0.5,alpha);
	if(wireFrame==1)
	   glutWireCone(.07, .2, 30, 30);
    else{
		glutSolidCone(.07, .2, 30, 30);
	}
	glColor4f(1,1,1,alpha);

	glScalef(1, 1, .5);
	glColor4f(1,0,0,alpha);
	if(wireFrame==1)
	   glutWireSphere(.08, 10, 30);
    else{
		if(showTexture){
			glColor4f(1,1,1, alpha);
			texture->drawTexturedSphere(4, .08, 10, 30);
		}
		else{
			glutSolidSphere(.08, 10, 30);
		}
	}
	glColor4f(1,1,1,alpha);
	glPopMatrix();

	// Front frame reflectors
	glPushMatrix();	
	glTranslatef(.5, .15, 0);
	glScalef(.05,.05,.7);
	glColor4f(0.5,0.5,0.5,alpha);
	if(wireFrame==1)		// bar
	   glutWireCube(.3);
    else{
		if(showTexture){
			glColor4f(1,1,1, alpha);
			texture->drawTexturedCube(7, .3);
		}
		else{
			glutSolidCube(.3);
		}
	}
	glColor4f(1,1,1,alpha);

	glColor4f(1,1,0,alpha);				// Right reflector
	glTranslatef(0.05, 0, .15);
	glScalef(1.5,2.5,.25);
	if(wireFrame==1)
	   glutWireSphere(.1, 30, 30);
    else{
		if(showTexture){
			glColor4f(1,1,1, alpha);
			texture->drawTexturedSphere(5, .1, 30, 30);
		}
		else{
			glutSolidSphere(.1, 30, 30);
		}
	}

	glTranslatef(0, 0, -1.15);		// Left reflector
	if(wireFrame==1)
	   glutWireSphere(.1, 30, 30);
    else{
		if(showTexture){
			glColor4f(1,1,1, alpha);
			texture->drawTexturedSphere(5, .1, 30, 30);
		}
		else{
			glutSolidSphere(.1, 30, 30);
		}
		
	}
	glColor4f(1,1,1,alpha);
	glPopMatrix();
}

// Function to draw the motorcycle's wheels
void Bike::drawMotorcycleWheels(float alpha){
	// Motorcycle Part 8: Wheels
    glPushMatrix();	
    glTranslatef(-.3,-.15,0);
	glColor4f(0.2,0.2,0.2,alpha);	
	if(wireFrame==1)
		glutWireTorus(.05,.1,10,50);	// Back wheel
	else{
		if(showTexture){
			glColor4f(1,1,1, alpha);
			texture->drawTexturedTorus(2, .05, .1, 10, 50);
		}
		else{
			glutSolidTorus(.05, .1, 10, 50);
		}
	}
	glColor4f(1,1,1,alpha);
			
	glColor4f(0.2,0.2,0.2,alpha);					// Back Wheel Hub cab
	if(wireFrame==1)					
	   glutWireSphere(.05, 5, 30);
    else{
		glutSolidSphere(.05, 5, 30);
	}
	glColor4f(1,1,1,alpha);

    glTranslatef(.9,0,0);				// Front wheel
	glColor4f(0.2,0.2,0.2,alpha);
	if(wireFrame==1)
		glutWireTorus(.05,.1,10,50);	
	else{
		if(showTexture){
			glColor4f(1,1,1, alpha);
			texture->drawTexturedTorus(2, .05, .1, 10, 50);
		}
		else{
			glutSolidTorus(.05, .1, 10, 50);
		}
	}
	glColor4f(1,1,1,alpha);
	
	glColor4f(0.2,0.2,0.2,alpha);					// Front Wheel Hub cab
	if(wireFrame==1)					
	   glutWireSphere(.05, 5, 30);
    else{
		glutSolidSphere(.05, 5, 30);
	}
	glColor4f(1,1,1,alpha);

	glScalef(.1,.1,.35);			// Front Wheel bar
	glColor4f(0.2,0.2,0.2,alpha);
	if(wireFrame==1)		
		glutWireCube(.3);
	else{
		glutSolidCube (.3);
	}

	glTranslatef(-9,0,0);			// Back Wheel bar
	glScalef(1,1,1.3);
	if(wireFrame==1)		
		glutWireCube(.3);
	else{
		glutSolidCube (.3);
	}
	glColor4f(1,1,1,alpha);
    glPopMatrix();
}